package com.kuangstudy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kuangstudy.entity.StateDetail;
import com.kuangstudy.entity.StateUserDetail;
import com.kuangstudy.service.detail.StateDetailService;
import com.kuangstudy.service.state.StateService;
import com.kuangstudy.service.stateuser.StateUserDetailService;
import com.kuangstudy.vo.R;
import com.kuangstudy.vo.StateDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @Author Melody_LQ
 * @Date 2021/6/28 11:19
 * @Description 统计项描述信息控制层
 * @Since version-1.0
 */
@Controller
public class StateDetailController {
    @Autowired
    private StateDetailService StateDetailService;
    @Autowired
    private StateService stateService;
    @Autowired
    private StateUserDetailService stateUserDetailService;


    /**
     * @Author Melody_LQ
     * @Date 2021/6/28 12:17
     * @Description 需求：根据用户点击所在的统计中的选项，进行一个累加的动作
     * @Param @param stateDetailVo: {stateid 统计id ; datailid 统计项id;}
     * @param session:
     * @Return com.kuangstudy.vo.R
     * @Since version-1.0
     */
    @PostMapping("/state/userdetail")
    @ResponseBody
    public R clickUserItem(@RequestBody StateDetailVo stateDetailVo, HttpSession session){
        //1、获取当前登录的用户id
        //Object userid = session.getAttribute("userid");
        Integer userid = 1; //模拟后台登录
        //2、根据统计统计id和项id和用户id查询是否已经点击投票过了
        LambdaQueryWrapper<StateUserDetail> queryWrapper = new LambdaQueryWrapper<StateUserDetail>();
        //jdk8 新特性  Lambda 使用 Xxxx::getXxxx 获取对象属性名方法 获取对象属性名
        queryWrapper.eq(StateUserDetail::getUserid,userid);
        queryWrapper.eq(StateUserDetail::getStateid,stateDetailVo.getStateid()); //stateDetailVo.getStateid()
        queryWrapper.eq(StateUserDetail::getDetailid,stateDetailVo.getDatailid());
        //3、根据queryWrapper条件查询 满足任意eq 就返回该对象StateUserDetail
        StateUserDetail stateDetailOne = stateUserDetailService.getOne(queryWrapper);
        //如果stateDetailOne不等于null就代表查询到了 已经更改了
        if(stateDetailOne == null){
            //4、对统计项进行一次累加
            StateDetail stateDetail = StateDetailService.getById(stateDetailVo.getDatailid());
            if (stateDetail != null){
                //5、更新用户点击的统计项的数量
                stateDetail.setItemnum(stateDetail.getItemnum()+1);
                StateDetailService.saveOrUpdate(stateDetail);
                //6、保存用户统计项的明细
                stateDetailOne = new StateUserDetail();
                stateDetailOne.setDetailid(stateDetailVo.getDatailid());
                stateDetailOne.setStateid(stateDetailVo.getStateid());
                stateDetailOne.setUserid(userid);
                stateUserDetailService.saveOrUpdate(stateDetailOne);
                return R.ok().message("投票成功!");
            }
        }
        return  R.error().message("你已投过票了!");

    }
}
